package com.clear.honghefinance.biz.modules.dictionary.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.clear.honghefinance.biz.modules.dictionary.converter.DataDictionaryConverter;
import com.clear.honghefinance.biz.modules.dictionary.domain.entity.DataDictionary;
import com.clear.honghefinance.biz.modules.dictionary.domain.vo.DataDictionaryVO;
import com.clear.honghefinance.biz.modules.dictionary.enums.DataDictionaryCategoryEnum;
import com.clear.honghefinance.biz.modules.dictionary.mapper.DataDictionaryMapper;
import com.clear.honghefinance.biz.modules.dictionary.service.IDataDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clear.honghefinance.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author 程序员clear
 * @since 2024-08-20
 */
@Service
@Slf4j
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary> implements IDataDictionaryService {

    /**
     * 查询行业数据列表
     *
     * @return
     */
    @Override
    public List<DataDictionaryVO> listIndustry() {
        try {
            List<DataDictionary> data = listByDataTypeCode(DataDictionaryCategoryEnum.INDUSTRY.getCode());
            return DataDictionaryConverter.INSTANCE.convertDataDictionaryEntityToDataDictionaryVO(data);
        } catch (Exception e) {
            log.error("查询行业数据列表异常 {}", e.getMessage(), e);
            throw new BizException("查询行业数据列表异常");
        }
    }

    /**
     * 查询会计准则数据列表
     *
     * @return
     */
    @Override
    public List<DataDictionaryVO> listAccountingRules() {
        try {
            List<DataDictionary> data = listByDataTypeCode(DataDictionaryCategoryEnum.ACCOUNTING_RULES.getCode());
            return DataDictionaryConverter.INSTANCE.convertDataDictionaryEntityToDataDictionaryVO(data);
        } catch (Exception e) {
            log.error("查询会计准则数据列表异常 {}", e.getMessage(), e);
            throw new BizException("查询会计准则数据列表异常");
        }
    }

    /**
     * 根据数据字典类型编码查询数据字典列表
     *
     * @param code 数据字典类型编码
     * @return 数据字典列表
     */
    private List<DataDictionary> listByDataTypeCode(String code) {
        return this.list(Wrappers.<DataDictionary>lambdaQuery()
                .eq(DataDictionary::getDataCodeCate, code)
                .eq(DataDictionary::getDisable, false)
                .orderByAsc(DataDictionary::getDataSort)
        );
    }
}
